<?php

namespace App\Sys\Controllers;

use App\Http\Controllers\BaseController;
use App\Sys\Services\SysI18nService;
use Illuminate\Http\Request;

class SysI18nController extends BaseController
{
    private $service;

    /**
     * SysI18nController constructor.
     * @param SysI18nService $service
     */
    public function __construct(SysI18nService $service)
    {
        $this->service = $service;
    }

    /**
     * @api post /api/basic/translate 翻译接口
     * @group 系统公用 系统公用
     * @param string q 关键词
     * @param string from 源语言
     * @param string to 目标语言
     * @return mixed
     * @successExample
     *
     * @desc
     *
     */
    public function translate(Request $request)
    {
        $q = $request->input('q');
        $from = $request->input('from', 'auto');
        $to = $request->input('to');
        return $this->service->translate($q, $from, $to);
    }

    /**
     * 获取资源
     * @param Request $request
     * @return \Illuminate\Support\Collection
     */
    public function getAppResource(Request $request)
    {
        if (env('APP_ENV') != 'local') {
            return null;
        }
        $appId = $request->input('app_id');
        $module = $request->input('module');
        return $this->service->getAppResource($appId, $module);
    }

    /**
     * 批量保存
     * @param Request $request
     * @return bool
     */
    public function saveAll(Request  $request)
    {
        if (env('APP_ENV') != 'local') {
            return null;
        }
        return $this->service->saveResources($request->input('data'));
    }

    /**
     * @api get /api/i18n/resource-query 国际化资源检索
     * @group 客户端 国际化
     * @param string app_id 应用ID
     * @param string module 模块
     * @param string key 键
     * @param string audit_lang 审核语言
     * @param string content 中文内容
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     * @successExample
     * {"ret":0,"msg":"success.","data":1}
     */
    public function lists(Request $request)
    {
        $appId = $request->input('app_id');
        $module = $request->input('module');
        $key = $request->input('key');
        $auditLang = $request->input('audit_lang');
        $content = $request->input('content');
        $pageSize = $this->getPageSize($request);
        return $this->service->listResource($appId, $module, $key, $content, $auditLang, $pageSize);
    }

    /**
     * @api get /api/i18n/resource-locale 获取国际化资源文件
     * @group 客户端 国际化
     * @param string app_id 应用ID
     * @param string locale 本地化语言
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     * @successExample
     * {"ret":0,"msg":"success.","data":1}
     */
    public function getAppLocale(Request $request)
    {
        $appId = $request->input('app_id');
        $locale = $request->input('locale');
        return $this->service->getAppLocale($appId, $locale);
    }

    /**
     * @api post /api/i18n/resource-edit 国际化资源修改
     * @group 客户端 国际化
     * @param string id 主键
     * @param string zh_tw 繁体
     * @param string en_us 英文
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     * @successExample
     *
     */
    public function save(Request $request)
    {
        $this->validate($request ,[
            'id' => 'required'
        ],[
            'id.required' => trans('i18n.id_empty')
        ]);
        $data = $request->all();
        $res = $this->service->saveOrUpdate($data);
        if ($res) {
            $old = $this->service->findOneById($data['id']);
            $someList = $this->service->findBy(['zh_cn'=>$old['zh_cn'], ['id', '<>', $old['id']]], 'id');
            foreach ($someList as &$some) {
                $data['id'] = $some['id'];
                $this->service->saveOrUpdate($data);
            }
        }
    }

    /**
     * @api post /api/i18n/resource-confirm 机器翻译确认
     * @group 客户端 国际化
     * @param string resource_ids 资源ID
     * @return mixed
     * @throws \App\Base\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     * @successExample
     */
    public function confirm(Request $request)
    {
        $this->validate($request, [
            'resource_ids' => 'required'
        ],[
            'resource_ids.required' => trans('i18n.resource_ids_empty')
        ]);
        $resourceIds = $request->input('resource_ids');
        $this->service->confirmMachineTrans($resourceIds);
    }

}
